package com.example.project.service.impl;

import com.example.project.dao.TableADao;
import com.example.project.dao.TableBDao;
import com.example.project.dao.TableCDao;
import com.example.project.entity.TableC;
import com.example.project.service.TableCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Ethan on 2022-08-25 22:16
 */
@Slf4j
@Service
public class TableCServiceImpl implements TableCService {
    @Autowired
    private TableADao tableADao;
    @Autowired
    private TableBDao tableBDao;
    @Autowired
    private TableCDao tableCDao;

    /**
     * 数据库查找所有数据
     * @return
     */
    @Override
    public List<TableC> findAll() {
        return tableCDao.findAll();
    }

    /**
     * 模糊查找
     * @param s
     * @return
     */
    @Override
    public List<TableC> findByName(String s) {
        return tableCDao.findByName(s);
    }

    /**
     * 计算并保存
     * @param
     */
    @Override
    public void computeAndSave(String s) {
        TableC tableC = new TableC();
        tableC.setB(s);
        int sumOfCoAa = tableBDao.getSumOfCoAa();
        int sumOfAa = tableADao.getSumOfAa();
        double aaS = (double) sumOfCoAa / sumOfAa;
        String aaSs = String.format("%.2f", aaS * 100) + " %";
        log.info(aaSs);
        tableC.setAaS(aaSs);

        int sumOfCoBb = tableBDao.getSumOfCoBb();
        int sumOfBb = tableADao.getSumOfBb();
        double bbS = (double) sumOfCoBb / sumOfBb;
        String bbSs = String.format("%.2f",bbS * 100) + "%";
        log.info(bbSs);
        tableC.setBbS(bbSs);

        int sumOfCoCc = tableBDao.getSumOfCoCc();
        int sumOfCc = tableADao.getSumOfCc();
        double ccS = (double) sumOfCoCc / sumOfCc;
        String ccSs = String.format("%.2f",ccS * 100) + "%";
        log.info(ccSs);
        tableC.setCcS(ccSs);

        int sumOfAa1 = tableBDao.getSumOfAa();
        double aaA = (double) sumOfCoAa / sumOfAa1;
        String aaAs = String.format("%.2f",aaA * 100) + "%";
        log.info(aaAs);
        tableC.setAaA(aaAs);

        int sumOfBb1 = tableBDao.getSumOfBb();
        double bbA = (double) sumOfCoBb / sumOfBb1;
        String bbAs = String.format("%.2f",bbA * 100) + "%";
        log.info(bbAs);
        tableC.setBbA(bbAs);

        int sumOfCc1 = tableBDao.getSumOfCc();
        double ccA = (double) sumOfCoCc / sumOfCc1;
        String ccAs = String.format("%.2f",ccA * 100) + "%";
        log.info(ccAs);
        tableC.setCcA(ccAs);

        int sumOfCoEe = tableBDao.getSumOfCoEe();
        int sumOfEe = tableADao.getSumOfEe();
        double aaSS = (double) sumOfCoEe / sumOfEe;
        String aaSSs = String.format("%.2f",aaSS * 100) +"%";
        log.info(aaSSs);
        tableC.setAaSS(aaSSs);

        double aaC =  (double) (sumOfCoAa + sumOfCoEe) / (sumOfAa + sumOfEe);
        String aaCs = String.format("%.2f",aaC * 100) + "%";
        log.info(aaCs);
        tableC.setAaC(aaCs);

        int sumOfCoDd = tableBDao.getSumOfCoDd();
        int sumOfDd = tableADao.getSumOfDd();
        double ddS = (double) sumOfCoDd / sumOfDd;
        String ddSs = String.format("%.2f",ddS * 100) + "%";
        log.info(ddSs);
        tableC.setDdS(ddSs);

        tableCDao.save(tableC);
    }
}
